package online.xybh.qa.service.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import online.xybh.qa.model.Student;
import online.xybh.qa.model.StudentExample;
import online.xybh.qa.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: xybh
 * @Description:
 * @Date: Created in 23:11 2020/10/20
 * @Modified:
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private online.xybh.qa.dao.StudentMapper studentMapper;
    @Override
    public PageInfo<Student> queryAllStudent(Integer page, Integer size) {
        StudentExample studentExample = new StudentExample();
        studentExample.setOrderByClause("student_id asc");
        PageHelper.startPage(page, size);
        List<Student> students = studentMapper.selectByExample(studentExample);
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        return studentPageInfo;
    }

    @Override
    public void addStudent(String stuId, String stuName, String stuClass) {
        Student student = new Student();
        student.setStudentId(stuId);
        student.setName(stuName);
        student.setClassId(stuClass);
        student.setPassword(stuId);
        student.setQuestion(0);
        student.setAvatarUrl("/img/logo.jpg");
        studentMapper.insertSelective(student);
    }

    @Override
    public PageInfo<Student> queryStudentByClassId(String classId, Integer page, Integer size) {
        StudentExample studentExample = new StudentExample();
        studentExample.createCriteria()
                .andClassIdEqualTo(classId);
        studentExample.setOrderByClause("student_id asc");
        PageHelper.startPage(page, size);
        List<Student> students = studentMapper.selectByExample(studentExample);
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        return studentPageInfo;
    }

    @Override
    public Student checkStudent(String username, String password) {
        Student student = studentMapper.selectByPrimaryKey(username);
        if (student.getPassword().equals(password)){
            online.xybh.qa.model.Student updateLogin = new Student();
            BeanUtils.copyProperties(student, updateLogin);
            updateLogin.setLastLoginTime(new Date());
            studentMapper.updateByPrimaryKey(updateLogin);
            return student;
        }
        return null;
    }
}
